﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VietThuongLMS.ViewModels;
using VietThuongLMS.Utils;

///Author: NHDuc
///Created date: 22/11/2013
namespace VietThuongLMS.Services
{
    public class TinTucService
    {
        public List<TinTuc> GetTinTucList()
        {
            try
            {
                using (LMSDataEntities context = new LMSDataEntities())
                {
                    List<TinTuc> list = context.TinTucs.ToList();
                    return list;
                }
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountServices::GetTinTucList", ex.StackTrace, ex.Message);
                return null;
            }
        }

        public static TinTucViewModel GetTinTucList(int pageNum, int pageIndex)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                List<TinTuc> list = context.TinTucs.Where(t => t.Deleted == Constant.GENERAL_STATUS_NORMAL).OrderByDescending(m => m.NgayDang).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                foreach (TinTuc item in list)
                {
                    if (item.NoiDung.Length > 400)
                    {
                        item.NoiDung = item.NoiDung.Substring(0, 399) + "...";
                    }
                }
                int total = context.TinTucs.Count();
                return new TinTucViewModel(list, total);

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountService::GetTinTucList", ex.StackTrace, ex.Message);
                return new TinTucViewModel();
            }
            finally
            {
                context.Dispose();
            }
        }

        public static TinTucViewModel GetTinTucList(int pageNum, int pageIndex, int id)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                List<TinTuc> list = context.TinTucs.Where(t => t.Deleted == Constant.GENERAL_STATUS_NORMAL
                        && t.MaLoaiTinTuc == id).OrderByDescending(m => m.NgayDang).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                foreach (TinTuc item in list)
                {
                    if (item.NoiDung.Length > 400)
                    {
                        item.NoiDung = item.NoiDung.Substring(0, 399) + "...";
                    }
                }
                int total = context.TinTucs.Count();
                LoaiTinTuc loai = context.LoaiTinTucs.FirstOrDefault(l => l.ID == id);
                string TenLoaiTinTuc = "";
                if (loai != null)
                    TenLoaiTinTuc = loai.TenLoaiTinTuc;
                return new TinTucViewModel(list, total, TenLoaiTinTuc);

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountService::GetTinTucList", ex.StackTrace, ex.Message);
                return new TinTucViewModel();
            }
            finally
            {
                context.Dispose();
            }
        }

        public static TinTucViewModel GetTinTucByMonthList(int month, int year, int pageNum, int pageIndex)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                List<TinTuc> list = context.TinTucs.Where(t => t.Deleted == Constant.GENERAL_STATUS_NORMAL
                    && t.NgayDang.Value.Month == month && t.NgayDang.Value.Year == year)
                    .OrderByDescending(m => m.NgayDang).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                int total = context.TinTucs.Where(t => t.Deleted == Constant.GENERAL_STATUS_NORMAL
                    && t.NgayDang.Value.Month == month && t.NgayDang.Value.Year == year).Count();
                return new TinTucViewModel(list, total);
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountService::GetTinTucByMonthList", ex.StackTrace, ex.Message);
                return new TinTucViewModel();
            }
            finally
            {
                context.Dispose();
            }
        }

        public static TinTucViewModel GetTinTucPopularList(int pageNum, int pageIndex)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                List<TinTuc> list = context.TinTucs
                    .Where(t => t.Deleted == Constant.GENERAL_STATUS_NORMAL).OrderByDescending(m => m.LuotXem)
                    .Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                int total = context.TinTucs.Count();
                return new TinTucViewModel(list, total);

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountService::GetTinTucPopularList", ex.StackTrace, ex.Message);
                return new TinTucViewModel();
            }
            finally
            {
                context.Dispose();
            }
        }

        public static TinTucViewModel GetTinTucRecentList(int pageNum, int pageIndex)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                List<TinTuc> list = context.TinTucs
                    .Where(t => t.Deleted == Constant.GENERAL_STATUS_NORMAL).OrderByDescending(m => m.NgayDang)
                    .Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                int total = context.TinTucs.Count();
                return new TinTucViewModel(list, total);

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountService::GetTinTucRecentList", ex.StackTrace, ex.Message);
                return new TinTucViewModel();
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool InsertTinTuc(TinTuc comp)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                context.AddToTinTucs(comp);
                context.SaveChanges();
                return true;

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("TinTucServices::InsertTinTuc", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool UpdateTinTucLuotView(int id)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                TinTuc tinTuc = context.TinTucs.FirstOrDefault(t => t.ID == id);
                if (tinTuc != null)
                {
                    tinTuc.LuotXem++;
                    context.SaveChanges();
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("TinTucServices::UpdateTinTucLuotView", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool Edit(TinTuc newModel)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                TinTuc model = context.TinTucs.Where(c => c.ID == newModel.ID).FirstOrDefault();
                if (model != null)
                {
                    model.TieuDe = newModel.TieuDe;
                    model.NoiDung = newModel.NoiDung;
                    model.MaLoaiTinTuc = newModel.MaLoaiTinTuc;
                    if (newModel.UrlAvatar != null && newModel.UrlAvatar != "")
                        model.UrlAvatar = newModel.UrlAvatar;
                    context.SaveChanges();
                    return true;
                }
                return false;

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("TinTucServices::Edit", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static TinTuc GetTinTucById(int id)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                return context.TinTucs.Where(c => c.ID == id).FirstOrDefault();

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("TinTucServices::GetTinTucById", ex.StackTrace, ex.Message);
                return null;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool DeleteTinTuc(int id)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                TinTuc obj = context.TinTucs.Where(c => c.ID == id).FirstOrDefault();
                if (obj != null)
                {
                    obj.Deleted = Utils.Constant.GENERAL_STATUS_DELETED;
                    context.SaveChanges();
                    return true;
                }
                return false;

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("TinTucServices::DeleteTinTuc", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }



    }
}